<?php

namespace App\Controller\App;

use App\Base\BaseController4Restful;
use App\Entity\UserSearch;
use App\Repository\AnnounceRepository;
use App\Repository\ArticleRepository;
use App\Repository\CategoryRepository;
use App\Repository\ProductRepository;
use App\Repository\ShopRepository;
use App\Repository\UserSearchRepository;
use Symfony\Component\Routing\Annotation\Route;
use function Doctrine\ORM\QueryBuilder;

class SearchController extends BaseController4Restful
{
    /**
     * @Route("/app/search", name="AppSearch")
     */
    public function AppSearch(
        ShopRepository $shopRepository
        ,ProductRepository $productRepository
        ,UserSearchRepository $userSearchRepository
    )
    {
        if(!$keywords = $this->getParamRequired("keywords")){return $this->json_out(201,"关键词不能为空");}
        if(!$page = $this->getParamRequired("page")){$page = 1;}
        //搜索商家
        $qb = $shopRepository->createQueryBuilder("S");
        $qb->where($qb->expr()->orX(
            $qb->expr()->like("S.name","'%{$keywords}%'")
            ,$qb->expr()->like("S.intro","'%{$keywords}%'")
            ,$qb->expr()->like("S.address","'%{$keywords}%'")
        ));
        $qb->setMaxResults(100);
        $qb->setFirstResult($page * 100 - 100);
        $data["shop"] = $qb->getQuery()->execute();

        //搜索服务
        $qb = $productRepository->createQueryBuilder("P");
        $qb->where($qb->expr()->orX(
            $qb->expr()->like("P.name","'%{$keywords}%'")
            ,$qb->expr()->like("P.intro","'%{$keywords}%'")
        ));
        $qb->setMaxResults(100);
        $qb->setFirstResult($page * 100 - 100);
        $data["product"] = $qb->getQuery()->execute();
        $this->setGroups("product","home");

        //记录搜索数据
        $user =$this->getUser();
        $search = $userSearchRepository->findOneBy(array("keywords"=>$keywords,"user"=>$user));
        if (!$search) {
            $UserSearch = new UserSearch();
            $UserSearch->setKeywords($keywords)
                ->setCreateAt(new \DateTime())
                ->setUser($user)
                ->setIsHot(0);
            $this->myEm()->persist($UserSearch);
            $this->myEm()->flush();
        }

        return $this->json_out(200,"OK",$data);
    }


    /**
     * @Route("/app/search/keys", name="AppSearchKeys")
     */
    public function AppSearchKeys(UserSearchRepository $userSearchRepository)
    {
        $user =$this->getUser();
        if ($user) {
            $data["my"] = $userSearchRepository->findBy(array("user"=>$user),array("id"=>"DESC"),10,0);
        }else{
            $data["my"] =[];
        }

        $data["isHot"] = $userSearchRepository->findBy(array("isHot"=>1),array("id"=>"DESC"),10,0);
        return $this->json_out(200,"数据获取成功",$data);
    }


    /**
     * @Route("/app/search/art", name="AppSearchArt")
     */
    public function AppSearchArticle(UserSearchRepository $userSearchRepository,ArticleRepository $articleRepository)
    {
        if(!$keywords = $this->getParamRequired("keywords")){return $this->json_out(201,"关键词不能为空");}
        //记录搜索数据
        $user =$this->getUser();
        $search = $articleRepository->search($keywords);
        if (!$search) {
            $UserSearch = new UserSearch();
            $UserSearch->setKeywords($keywords)
                ->setCreateAt(new \DateTime())
                ->setUser($user)
                ->setIsHot(0);
            $this->myEm()->persist($UserSearch);
            $this->myEm()->flush();
        }
        return $this->json_out(200,"数据获取成功",$search);
    }
}
